require 'rubygems'
require 'test/unit'
require 'mocha'
require 'agent'
require 'server'
require 'socket'
require 'database'
require 'remote_agent'
require 'fileutils'
include FileUtils

class TestEndToEnd < Test::Unit::TestCase

  def setup
    Database.test_connect
    SharedFile.stubs(:fetch_content).returns(mock())
    @server = Server.instance()
    @agent = Agent.instance()
    @server.start
    @agent.start
  end

  def teardown
    @server.stop
    @agent.stop
  end

  def test_server_can_write_remote_files
    @agent.interface.publish_to_server
    File.open("file.tmp", "w"){ |f| f.puts("overwrite me.") }
    file = RemoteFile.find_by_path("/path/to/config")
    file.path = "file.tmp"
    @agent.interface.instance_variable_get(:@files)[0].path = "file.tmp"
    file.update_content(@agent.interface, "testing\n123")
    content = nil
    File.open("file.tmp") { |f| content = f.readlines }
    assert_equal("testing", content[0].strip)
    assert_equal("123", content[1].strip)
    rm "file.tmp"
  end

end
